<?php
/**
 * Created by PhpStorm.
 * User: xxt
 * Date: 18-6-19
 * Time: 下午2:52
 */

namespace app\index\controller;

use GuzzleHttp\Client;
use GuzzleHttp\Cookie\SetCookie;

class ApiCaptcha
{
    protected $client;
    protected $jar;
    protected $tmp;

    public function __construct()
    {
        session_start();
        $this->tmp = new SetCookie;
        $this->tmp->setName('PHPSESSID');
        $this->tmp->setValue(session_id());
        $this->tmp->setDomain(Config('captchaUrl'));
        $this->tmp->setExpires(3600);
        $this->tmp->setHttpOnly(true);
        $this->jar = new \GuzzleHttp\Cookie\CookieJar;
        $this->jar->setCookie($this->tmp);
        $this->client = new Client([
            'base_uri' => Config('captchaUrl'),
            'headers' => [
                'Connection' => 'Keep-Alive'
            ],
            'cookies' => $this->jar
        ]);
    }

    public function captcha_get()
    {
        dump($this->client);
        $response = $this->client->request('GET', "/d8/captcha/generate");
        dump(session_id());
        if (!empty($response)) return $response->getBody()->getContents();
        else return array('不存在此消息');
    }

    public function captcha_submit($data)
    {
        $response = $this->client->request('POST', "/d8/captcha/add_suggestion", ['form_params' => $data]);
        if (!empty($response)) return $response->getBody()->getContents();
        else return array('不存在此消息');
    }

    public function captcha_getrand()
    {
        $response = $this->client->request('GET', 'd8/captcha/getrand');
        return $response->getBody()->getContents();
    }
}